package com.bree.erp.jsf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.service.ItemRequestService;
import com.bree.erp.service.PurchaseRequisitionService;

/**
 * @author bringinoo
 *
 */
@Deprecated
public class ItemRequestListBean {
	
	private static Logger LOG = Logger.getLogger(ItemRequestListBean.class);

	@EJB
	private PurchaseRequisitionService prService;
	
	@EJB
	private ItemRequestService irService;
	
	private PurchaseRequisitionBean detailBean;
	private PurchaseRequisition purchaseRequisition;
	private long prId;
	

	private PurchaseRequisition getSessionPurchaseRequisition() {
		FacesContext context = FacesContext.getCurrentInstance();

		PurchaseRequisition session = (PurchaseRequisition) context.getApplication().evaluateExpressionGet(context, "#{purchaseRequisitionBean}", PurchaseRequisition.class);
		
		return session;
	}
	
	@PostConstruct
	public void init() {
		
		LOG.debug("@PostConstruct: PurchaseRequisitionDetailBean");
		
		PurchaseRequisition session = getSessionPurchaseRequisition();
		
		purchaseRequisition = new PurchaseRequisition();
		PurchaseRequisition.copy(session, purchaseRequisition);
	}
	
	public List<ItemRequest> getRequests() {
		
		//PurchaseRequisition fresh = prService.read(this.purchaseRequisition.getId());

		List<ItemRequest> list = irService.list(purchaseRequisition.getId());
				
		//list.addAll(purchaseRequisition.getRequests());
		
		LOG.debug("list count: " + list.size());
		
		return list;
		
	}

	
	public void deleteRequestItem(long id) {
		
		LOG.debug("deleteRequestItem(" + id + ")");
		
		irService.delete(id);
		
		// update session's copy.
		PurchaseRequisition session = getSessionPurchaseRequisition();
		PurchaseRequisition updated = prService.read(session.getId(), true);
		PurchaseRequisition.copy(updated, session);
		// and this bean's copy
		PurchaseRequisition.copy(updated, this.purchaseRequisition);
		
		this.getRequests();

		LOG.debug(":deleteRequestItem(" + id + ")");

	}

	
	public boolean getRenderDelete() {
		PurchaseRequisition session = getSessionPurchaseRequisition();
		
		if(session.getStatus() == Status.OPEN ) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @return the purchaseRequisition
	 */
	public PurchaseRequisition getPurchaseRequisition() {
		return purchaseRequisition;
	}

	/**
	 * @param purchaseRequisition the purchaseRequisition to set
	 */
	public void setPurchaseRequisition(PurchaseRequisition purchaseRequisition) {
		this.purchaseRequisition = purchaseRequisition;
		prId = purchaseRequisition.getId();
	}


	/**
	 * @return the prId
	 */
	public long getPrId() {
		return prId;
	}


	/**
	 * @param prId the prId to set
	 */
	public void setPrId(long prId) {
		this.prId = prId;
	}


	/**
	 * @return the detailBean
	 */
	public PurchaseRequisitionBean getDetailBean() {
		return detailBean;
	}


	/**
	 * @param detailBean the detailBean to set
	 */
	public void setDetailBean(PurchaseRequisitionBean detailBean) {
		this.detailBean = detailBean;
	}
	
	
}
